package com.college.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.college.domain.Roles;
import com.college.domain.Users;
import com.college.interfaces.IUserDAO;

@Repository
@Transactional
public class UserDAO implements IUserDAO {
	

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	public List<Integer> getAllUserIdForDropDown() {
		List<Users> users = getAllUsers();
		ArrayList<Integer> userIdList = new ArrayList<Integer>();
		for(Users user :users){
			userIdList.add(user.getUserId());
		}
		return userIdList;
	}

	@Override
	public List<Users> getAllUsers() {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Users.class);		
		return criteria.list();
	}

	@Override
	public Integer saveUser(Users user) {
		
		return (Integer) sessionFactory.getCurrentSession().save(user);
	}

	@Override
	public List<Users> getUserByUsername(String userName) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Users.class);
		criteria.add(Restrictions.ilike("userName", userName));
		return  criteria.list();
	}

	
}
